package com.sunseagear.wind.modules.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.sys.entity.UserRole;
import com.sunseagear.wind.modules.sys.service.IUserRoleService;
import com.sunseagear.wind.common.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 *
 * @version V1.0
 * @package com.sunseagear.upms.controller
 * @title: 用户角色控制器
 * @description: 用户角色控制器 * @date: 2018-05-06 18:46:48
 * @copyright: 2018 www.sunseagear.com Inc. All rights reserved.
 */
@RestController
@RequestMapping("/sys/user/role")
@Log(title = "用户管理")
@RequiredArgsConstructor
public class UserRoleController extends BaseBeanController {

    private final IUserRoleService userRoleService;


    /**
     * 获取用户角色
     * @param userId 用户id
     * @return 拥有的角色列表
     */
    @GetMapping(value = "roleIds/{userId}")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("sys:user:role:list")
    public R<List<Long>> userRoleIds(@PathVariable("userId") Long userId) {
        List<Long> roleIdList = new ArrayList<>();
        List<UserRole> userRoleList = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userId));
        for (UserRole userRole : userRoleList) {
            roleIdList.add(userRole.getRoleId());
        }
        return R.success(roleIdList);
    }

    /**
     * 新增关系
     */
    @PostMapping("insertByUserId/{userId}")
    @Log(logType = LogType.INSERT)
    @SaCheckPermission("sys:role:add")
    public R<Void> insertByUserId(@PathVariable("userId") Long userId,@RequestBody List<Long> roleIds) {
        for (Long roleId : roleIds) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoleService.insertByRoleId(userId, roleId);
        }
        UserUtils.clearCache(Objects.requireNonNull(UserUtils.get(userId)));
        return R.success("添加成功");
    }

    /**
     * 删除关系
     */
    @DeleteMapping("deleteByUserId/{userId}")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("sys:role:delete")
    public R<Void> deleteByUserId(@PathVariable("userId") Long userId, @RequestBody List<String> roleIds) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.in("role_id", roleIds);
        userRoleService.remove(queryWrapper);
        UserUtils.clearCache(Objects.requireNonNull(UserUtils.get(userId)));
        return R.success("删除成功");
    }

}
